package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Meeting;
import com.zyn.rsgl.service.MeetingService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 会议表 前端控制器
 * </p>
 *
 * @author zyn
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/rsgl/meeting")
public class MeetingController {
    @Resource
    private MeetingService meetingService;

    // 查询所有
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false) String name,
                  @RequestParam(required = false) String time,
                  @RequestParam(required = false) String status,
                  @RequestParam(required = false) String location) {
        return meetingService.listPage(pageNum, pageSize, name, time, status, location);
    }

    // 根据ID获取详细信息
    @GetMapping("/get/{meetingId}")
    public R get(@PathVariable("meetingId") String meetingId) {
        Meeting byId = meetingService.getById(meetingId);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("未查询到该会议");
    }

    // 添加
    @PostMapping("/add")
    public R add(@RequestBody Meeting meeting) {
        // 判断是否存在
        if (meetingService.getById(meeting.getId()) != null) {
            return R.fail("该会议已存在");
        }
        return meetingService.save(meeting) ? R.ok() : R.fail("添加失败");
    }

    // 修改
    @PutMapping("/update")
    public R update(@RequestBody Meeting meeting) {
        return meetingService.updateById(meeting) ? R.ok() : R.fail("修改失败");
    }

    // 删除
    @DeleteMapping("/delete/{meetingId}")
    public R delete(@PathVariable("meetingId") String meetingId) {
        return meetingService.removeById(meetingId) ? R.ok() : R.fail("删除失败");
    }
}
